#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace TVC.Modules.CustomControl
{
    public partial class myGroupBox : UserControl
    {
        public DataTable dt;
        System.ComponentModel.ComponentResourceManager resources;
        const int marginBot = 10;         
        public myGroupBox()
        {
            InitializeComponent();
            resources = new System.ComponentModel.ComponentResourceManager(typeof(myGroupBox));
            this.pibExpand.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle("Expand.png");
        }

        public void SetHeaderText(string header, int intleft, int inttop, int intheight, bool pibEx)
        {
            label1.Text = header;
            label1.Padding = new Gizmox.WebGUI.Forms.Padding(intleft, inttop, 0, 0);
            if (!pibEx)
                this.label1.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))));
            //label1.Margin = new Gizmox.WebGUI.Forms.mar (intleft, inttop, 0, 0);
            ResizeHeader(header, intheight);
            pibExpand.Visible = pibEx;
        }
        
        private void ResizeHeader(string header, int intheight) 
        {
            if (header != "")
            {
                Graphics g;
                Bitmap TextBitmap = new Bitmap(1, 1);
                g = Graphics.FromImage(TextBitmap);
                float width = g.MeasureString(header, label1.Font).Width;
                int ln = Convert.ToInt32(width / this.Width);
                float height = g.MeasureString(header, label1.Font).Height * (ln + 1) + 2;
                plnTop.Height = Convert.ToInt32(height);
                this.Height = plnTop.Height + marginBot + intheight;
            }
        }
        
        public string GetSelected()
        {
            foreach (Control x in plnControl.Controls)
            {
                if (x is RadioButton)
                {
                    if (((RadioButton)x).Checked)
                        return x.Name;
                }
            }
            return "";
        }
        public void SetSelected(string value)
        {
            foreach (Control x in plnControl.Controls)
            {
                if (x is RadioButton)
                {
                    if (x.Name == value)
                    {
                        ((RadioButton)x).Checked = true;
                    }
                }
            }
        }

        public void SetContent(Dictionary<string, string> items)
        {
            foreach (KeyValuePair<string, string> key in items)
            {
                RadioButton rbt = new RadioButton();
                rbt.Text = key.Value;
                rbt.Name = key.Key;
                //rbt.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
                rbt.Dock = Gizmox.WebGUI.Forms.DockStyle.Top;
                rbt.Padding = new Gizmox.WebGUI.Forms.Padding(30, 0, 0, 0);
                plnControl.Controls.Add(rbt);
            }
        }
        
        public void AddContent(string key, string value)
        {
            if (!plnControl.Controls.ContainsKey(key))
            {
                RadioButton rbt = new RadioButton();
                rbt.Text = value;
                rbt.Name = key;
                //rbt.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
                rbt.Dock = Gizmox.WebGUI.Forms.DockStyle.Top;
                rbt.Padding = new Gizmox.WebGUI.Forms.Padding(40, 0, 0, 0);
                //rbt.CheckedChanged += new EventHandler(Checked_Changed); 
                plnControl.Controls.Add(rbt);
            }
            else
            {
                plnControl.Controls[key].Text = value;
            }
        }
        public void Colapse()
        {
            //this.Height = plnTop.Height + marginBot;
            this.Height = 27;
            this.pibExpand.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle("Expand.png");
            plnControl.Visible = false;
        }
        
        public void Expand()
        {
            int height = 0;
            foreach (Control x in plnControl.Controls)
            {
                height += x.Height;
            }
            height += plnTop.Height;
            this.Height = height + marginBot ;
            this.pibExpand.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle("Toggle.png");
            plnControl.Visible = true;
        }
        
        private void pibExpand_Click(object sender, EventArgs e)
        {
            if (this.Height == 27)
            {
                Expand();
            }
            else
            {
                Colapse();
            }
        }
        
        //public void Resize()
        //{
        //    //ResizeHeader(label1.Text);
        //    //if (this.Height > plnTop.Height + marginBot)
        //    //    Expand();
        //}
    }
}